package model.behaviors;

import java.awt.Graphics2D;
import java.util.HashMap;
import java.util.List;
import model.Figure;
import model.Move;
import model.Positions;
import figures.Imgs;

/**
 * the pawn behavior
 */
public class Pawn extends AbstractBehavior implements Positions {

    /**
     * the constructor of this figure behavior
     * 
     * @param color
     *            the color to choose the right figure
     */
    public Pawn( int color )
    {
	super();

	if ( color == Figure.WHITE )
	{
	    this.img = Imgs.PAWN_W;
	}
	else
	{
	    this.img = Imgs.PAWN_B;
	}
    }

    @Override
    public final void detectPossibleMoves( int fs, Graphics2D g, HashMap<String, Figure> figMap, Figure figure,
	    List<Move> movesList )
    {
	// empty list of valid fields
	this.validFields.clear();

	// calc x/y
	int x = figure.getX();
	int y = figure.getY();

	/* figure behaviour */
	/* check if there is a figure right in front of the pawn */
	if ( !figMap.containsKey( convertXYToField( x, ( figure.isWhite() ) ? y - 1 : y + 1 ) ) )
	{
	    addToValidFields( x, ( figure.isWhite() ) ? y - 1 : y + 1 );

	    /* check if there is a figure two steps in front of the pawn (which is on the start position) */
	    if ( figure.getY() == ( ( figure.isWhite() ) ? 6 : 1 )
		    && !figMap.containsKey( convertXYToField( x, ( figure.isWhite() ) ? 4 : 3 ) ) )
	    {
		addToValidFields( x, ( figure.isWhite() ) ? 4 : 3 );
	    }
	}

	/* strike enemy figure */
	if ( figMap.containsKey( convertXYToField( x - 1, ( figure.isWhite() ) ? y - 1 : y + 1 ) ) )
	{
	    if ( figMap.get( convertXYToField( x - 1, ( figure.isWhite() ) ? y - 1 : y + 1 ) ).getColor() != figure
		    .getColor() )
	    {
		addToValidFields( x - 1, ( figure.isWhite() ) ? y - 1 : y + 1 );
	    }
	}
	if ( figMap.containsKey( convertXYToField( x + 1, ( figure.isWhite() ) ? y - 1 : y + 1 ) ) )
	{
	    if ( figMap.get( convertXYToField( x + 1, ( figure.isWhite() ) ? y - 1 : y + 1 ) ).getColor() != figure
		    .getColor() )
	    {
		addToValidFields( x + 1, ( figure.isWhite() ) ? y - 1 : y + 1 );
	    }
	}

	/* check en passant */
	// if last move was enemy pawn moving from his start position two fields up/down, then..
	// TODO ArrayIndexOutOfBoundsException, wenn movelist noch leer is
	if ( movesList.size() != 0 )
	{
	    String tempStart = movesList.get( movesList.size() - 1 ).getStartField();
	    String tempEnd = movesList.get( movesList.size() - 1 ).getEndField();
	    if ( 7 - ( tempStart.charAt( 1 ) - '1' ) == ( ( figure.isWhite() ) ? 1 : 6 )
		    && 7 - ( tempEnd.charAt( 1 ) - '1' ) == ( ( figure.isWhite() ) ? 3 : 4 )
		    && figMap.get( tempEnd ).getBehav() instanceof Pawn ) // TODO nullpointer
	    {
		// .. check if he is next to this pawn
		if ( figure.getY() == figMap.get( tempEnd ).getY() )
		{
		    // .. to the left?
		    if ( figure.getX() == figMap.get( tempEnd ).getX() + 1 )
		    {
			addToValidFields( figMap.get( tempEnd ).getX(), figure.isWhite() ? y + 1 : y - 1 );
		    }
		    // .. to the right?
		    if ( figure.getX() == figMap.get( tempEnd ).getX() - 1 )
		    {
			addToValidFields( figMap.get( tempEnd ).getX(), figure.isWhite() ? y + 1 : y - 1 );
		    }
		}
	    }
	}

	// end
	drawFields( fs, g );
    }
}
